import { Controller } from 'egg';

class signController extends Controller {
  async add() {
    const { ctx } = this;
    const prams:any = ctx.request.body;
    const result = await ctx.service.sign.add(prams).catch(() => {
      ctx.body = {
        code: 502,
        msg: '添加失败，服务器异常,请联系管理员',
      };
    });
    if (prams.num) {
      if (result.id) {
        ctx.body = {
          code: 200,
          msg: '添加成功',
          row: result,
        };
      } else {
        ctx.body = {
          code: 400,
          msg: '添加失败，服务器异常',
        };
      }
    } else {
      ctx.body = {
        code: 400,
        msg: '添加失败，参数错误',
      };
    }

  }
  async update() {
    const { ctx } = this;
    const prams:object[] = ctx.request.body;
    const result = await ctx.service.sign.update(prams).catch(() => {
      ctx.body = {
        code: 502,
        msg: '修改失败，服务器异常',
      };
    });
    if (result[0] > 0) {
      ctx.body = {
        code: 200,
        msg: '修改成功',
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '修改失败，服务器异常',
      };
    }
  }
  async query() {
    const { ctx } = this;
    const prams:object[] = ctx.request.body;
    const result = await ctx.service.sign.query(prams).catch(() => {
      ctx.body = {
        code: 502,
        msg: '查询失败，服务器异常',
      };
    });
    const res = JSON.parse(JSON.stringify(result.res));
    const addMoneyList = await ctx.service.dictionary.addMoneyList({ page: 1, size: 500 }).catch(() => {
      ctx.body = {
        code: 502,
        msg: '查询失败，服务器异常',
      };
    });
    const moneyList = JSON.parse(JSON.stringify(addMoneyList));
    const categoryList = await ctx.service.dictionary.categoryList({ page: 1, size: 500 }).catch(() => {
      ctx.body = {
        code: 502,
        msg: '查询失败，服务器异常',
      };
    });
    const category = JSON.parse(JSON.stringify(categoryList));
    const labelList = await ctx.service.dictionary.labelList({ page: 1, size: 500 }).catch(() => {
      ctx.body = {
        code: 502,
        msg: '查询失败，服务器异常',
      };
    });
    const label = JSON.parse(JSON.stringify(labelList));

    res.forEach(item => {
      moneyList.forEach(item2 => {
        if (item.sign && item.sign.addmoney === item2.id) {
          item.sign.tbaddmoney = {
            aname: item2.value,
          };
        }
      });
      category.forEach(item2 => {
        if (item.sign && item.sign.genre === item2.id) {
          item.sign.tbcategory = {
            cname: item2.cname,
          };
        }
      });
      label.forEach(item2 => {
        if (item.sign && item.sign.lable === item2.id) {
          item.sign.tblabel = {
            laname: item2.laname,
          };
        }
      });
    });
    console.log(res);
    if (res.length && result.count.length) {
      ctx.body = {
        code: 200,
        msg: '查询成功',
        row: res,
        count: result.count.length,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '查询失败，服务器异常',
      };
    }
  }
}

module.exports = signController;
